package com.sky.controller.admin;

import com.sky.result.Result;
import com.sky.service.StatisticsService;
import com.sky.vo.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletResponse;
import java.time.LocalDate;
import java.time.LocalDateTime;

@RestController
@Slf4j
@Api(tags = "数据统计相关接口")
@RequestMapping("/admin/report")
public class StatisticsController {

    @Autowired
    private StatisticsService statisticsService;
    @ApiOperation("营业额统计接口")
    @GetMapping("/turnoverStatistics")
    public Result<TurnoverReportVO> turnover(
            @DateTimeFormat(pattern = "yyyy-MM-dd")
            LocalDate begin,
            @DateTimeFormat(pattern = "yyyy-MM-dd")
            LocalDate end){

        log.info("开始时间:{} 结束时间:{}",begin,end);
        TurnoverReportVO turnoverReportVO=statisticsService.turnover(begin,end);
        return Result.success(turnoverReportVO);
    }

    @ApiOperation("用户统计")
    @GetMapping("/userStatistics")
    public Result<UserReportVO> UserReport(
            @DateTimeFormat(pattern = "yyyy-MM-dd")
            LocalDate begin,
            @DateTimeFormat(pattern = "yyyy-MM-dd")
            LocalDate end){
        UserReportVO userReportVO=statisticsService.UserReport(begin,end);
        return Result.success(userReportVO);
    }
    @ApiOperation("订单统计结果")
    @GetMapping("/ordersStatistics")
    public Result<OrderReportVO> OrderReport(
            @DateTimeFormat(pattern = "yyyy-MM-dd")
            LocalDate begin,
            @DateTimeFormat(pattern = "yyyy-MM-dd")
            LocalDate end){
        OrderReportVO orderReportVO=statisticsService.OrderReport(begin,end);
        return Result.success(orderReportVO);
    }

    @ApiOperation("销量排行TOP10接口")
    @GetMapping("/top10")
    public Result<SalesTop10ReportVO>  SalesTop10(
            @DateTimeFormat(pattern = "yyyy-MM-dd")
            LocalDate begin,
            @DateTimeFormat(pattern = "yyyy-MM-dd")
            LocalDate end){
        SalesTop10ReportVO salesTop10ReportVO= statisticsService.SalesTop10(begin,end);

        return Result.success(salesTop10ReportVO);
    }
    @ApiOperation("导出Excel报表接口")
    @GetMapping("/export")
    public void exportReport(HttpServletResponse response){
        statisticsService.export(response);
    }

}
